package com.example.administrator.live;

import android.webkit.WebView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/28.
 */

public class JsCallJava {
    String INJECT_JS = "window.EasyJS = {\n" +
            "\t__callbacks: {},\n" +
            "\t\n" +
            "\tinvokeCallback: function (cbID, removeAfterExecute){\n" +
            "\t\tvar args = Array.prototype.slice.call(arguments);\n" +
            "\t\targs.shift();\n" +
            "\t\targs.shift();\n" +
            "\t\t\n" +
            "\t\tfor (var i = 0, l = args.length; i < l; i++){\n" +
            "\t\t\targs[i] = decodeURIComponent(args[i]);\n" +
            "\t\t}\n" +
            "\t\t\n" +
            "\t\tvar cb = EasyJS.__callbacks[cbID];\n" +
            "\t\tif (removeAfterExecute){\n" +
            "\t\t\tEasyJS.__callbacks[cbID] = undefined;\n" +
            "\t\t}\n" +
            "\t\treturn cb.apply(null, args);\n" +
            "\t},\n" +
            "\t\n" +
            "\tcall: function (obj, functionName, args){\n" +
            "\t\tvar formattedArgs = [];\n" +
            "\t\tfor (var i = 0, l = args.length; i < l; i++){\n" +
            "\t\t\tif (typeof args[i] == \"function\"){\n" +
            "\t\t\t\tformattedArgs.push(\"f\");\n" +
            "\t\t\t\tvar cbID = \"__cb\" + (+new Date);\n" +
            "\t\t\t\tEasyJS.__callbacks[cbID] = args[i];\n" +
            "\t\t\t\tformattedArgs.push(cbID);\n" +
            "\t\t\t}else{\n" +
            "\t\t\t\tformattedArgs.push(\"s\");\n" +
            "\t\t\t\tformattedArgs.push(encodeURIComponent(args[i]));\n" +
            "\t\t\t}\n" +
            "\t\t}\n" +
            "\t\t\n" +
            "\t\tvar argStr = (formattedArgs.length > 0 ? \":\" + encodeURIComponent(formattedArgs.join(\":\")) : \"\");\n" +
            "\t\t\n" +
            "\t\tvar iframe = document.createElement(\"IFRAME\");\n" +
            "\t\tiframe.setAttribute(\"src\", \"easy-js:\" + obj + \":\" + encodeURIComponent(functionName) + argStr);\n" +
            "\t\tdocument.documentElement.appendChild(iframe);\n" +
            "\t\tiframe.parentNode.removeChild(iframe);\n" +
            "\t\tiframe = null;\n" +
            "\t\t\n" +
            "\t\tvar ret = EasyJS.retValue;\n" +
            "\t\tEasyJS.retValue = undefined;\n" +
            "\t\t\n" +
            "\t\tif (ret){\n" +
            "\t\t\treturn decodeURIComponent(ret);\n" +
            "\t\t}\n" +
            "\t},\n" +
            "\t\n" +
            "\tinject: function (obj, methods){\n" +
            "\t\twindow[obj] = {};\n" +
            "\t\tvar jsObj = window[obj];\n" +
            "\t\t\n" +
            "\t\tfor (var i = 0, l = methods.length; i < l; i++){\n" +
            "\t\t\t(function (){\n" +
            "\t\t\t\tvar method = methods[i];\n" +
            "\t\t\t\tvar jsMethod = method.replace(new RegExp(\":\", \"g\"), \"\");\n" +
            "\t\t\t\tjsObj[jsMethod] = function (){\n" +
            "\t\t\t\t\treturn EasyJS.call(obj, method, Array.prototype.slice.call(arguments));\n" +
            "\t\t\t\t};\n" +
            "\t\t\t})();\n" +
            "\t\t}\n" +
            "\t}\n" +
            "};";
    public Map<String,Object> map;

    public void addJavascriptInterfaces(Object obj,String name){
        if(map==null)
            map = new HashMap<>();
        map.put(name,obj);
    }

    public void onPageStarted(WebView view, String url) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String,Object> entry : map.entrySet()) {
            sb.append("EasyJS.inject(\"");
            sb.append(entry.getKey());
            sb.append("\", [");
            sb.append("\"");
            sb.append("test");
            sb.append("\"");
            sb.append("]);");
        }
        view.loadUrl("javascript:"+INJECT_JS);
        view.loadUrl("javascript:"+sb.toString());
    }
}
